// maindlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#if defined(_MSC_VER) && _MSC_VER >= 1200
#pragma comment(linker, "/OPT:NOWIN98" )
#endif

#include "stdafx.h"
#include "resource.h"

#include "aboutdlg.h"
#include "maindlg.h"
#include  "SystemHook/SystemHook.h"
#include "trayiconimpl.h"
#include <Psapi.h>

DWORD aProcesses[1024];

BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{
	return CWindow::IsDialogMessage(pMsg);
}

BOOL CMainDlg::OnIdle()
{
	return FALSE;
}

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// center the dialog on the screen
	CenterWindow();

	// set icons
	HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
	SetIcon(hIconSmall, FALSE);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	InstallIcon(_T("WTLTrayIconDialog"), hIconSmall, IDR_POPUP);
	StartProcessMgr(m_hWnd);

	UIAddChildWindowContainer(m_hWnd);

	return TRUE;
}

LRESULT CMainDlg::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}

LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add validation code 
	CloseDialog(wID);
	return 0;
}

LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CloseDialog(wID);
	return 0;
}

void CMainDlg::CloseDialog(int nVal)
{
	StopProcessMgr(m_hWnd);
	DestroyWindow();
	::PostQuitMessage(nVal);
}

void CMainDlg::PrepareMenu(HMENU hMenu)
{
	DWORD cbNeeded, cProcesses;

	if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
		return;
	cProcesses = cbNeeded / sizeof(DWORD);

	CMenuHandle menu(hMenu);
	CMenu hMenuPopup;
	hMenuPopup.CreatePopupMenu();

	hMenuPopup.AppendMenu(MF_STRING, 0x1100, L"&Kill");
	hMenuPopup.AppendMenu(MF_STRING, 0x1101, L"&Kill");
	hMenuPopup.AppendMenu(MF_STRING, 0x1102, L"&Kill");

	menu.InsertMenu(-1, MF_STRING | MF_POPUP, (UINT)hMenuPopup.m_hMenu, L"&Priority");
}